using LitJson2;
using Net.Common;
using UnityEngine;

/****************************************************
// 功能：登录业务
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2024/12/30 15:18:17
*****************************************************/

public class LoginController : BaseController<LoginController>
{
    public override void InitController()
    {
        base.InitController();
        //Debug.Log("LoginController Init");
    }

    /// <summary>
    /// 进入登录场景
    /// </summary>
    public void EnterLogin()
    {
        // 异步加载登录场景，显示加载进度条
        resource.AsyncLoadScene(Constants.SCREEN_LOGIN, () =>
        {
            //if (!model.isConnected) return;
            ui.Open(WindowName.Login, null);
            ui.PopUp(WindowName.Protocol, "点击查看<link='https://rule.tencent.com/rule/202406170001'><color=#79b2d7>游戏许可及服务协议</color></link>，" +
                                          "<link='https://rule.tencent.com/rule/d277ee20-2709-4cd5-882b-cbd485753346'><color=#79b2d7><color=#79b2d7>王者荣耀隐私保护指引</color></link>，" +
                                          "<link='https://rule.tencent.com/rule/202406180001'><color=#79b2d7>儿童隐私保护指引</color></link>，" +
                                          "<link='https://rule.tencent.com/rule/7e83a0a6-64a6-4397-947d-2984968da4f9'><color=#79b2d7>王者荣耀第三方信息共享清单</color></link>，" +
                                          "<link='https://rule.tencent.com/rule/202406170002'><color=#79b2d7>游戏账号注册协议</color></link>");
        });
    }

    /// <summary>
    /// 游客登录
    /// </summary>
    public void ClickYouKe()
    {
        var account = GetRandomName();
        SendByTcp(RequestType.LOGIN, new LoginRequest()
        {
            account = account,
            password = "1990911"
        });

        // 模拟游戏
        //SimulateController.Instance.StartSimulate();
    }
    
    /// <summary>
    /// 登录消息回调
    /// </summary>
    /// <param name="msg"></param>
    public void OnLogin(MsgInfo msg)
    {
        var jsonData = JsonMapper.ToObject(msg.data);

        var userInfo = JsonMapper.ToObject<UserInfo>(jsonData["userInfo"].ToJson());
        model.userInfo = userInfo;
        model.notice = jsonData["notice"].ToString();
        ui.Open(WindowName.Lobby);
        ui.DestoryWindow(WindowName.Login);
        ui.ShowTips("登录成功");
        
        SendByKcp(RequestType.KCP_LOGIN, new LoginOrCancelRequest()
        {
            type = Constants.NUM_ZERO
        });
    }

    public void OnConnectionRefused(MsgInfo msg)
    {
        ui.ShowTips(msg.data);
    }

    private static string GetRandomName()
    {
        string[] names = {
            "独断万古荒天帝", "天骄剑仙", "紫云神尊", "飞天御剑", "霜月仙子", "龙吟九天", "瑶华仙子", "星辰之道", "风华绝代", "青莲圣尊", 
            "浩天仙帝", "逸尘剑神", "落霞神女", "幻影无痕", "苍松道长", "雷霆战神", "断岳无敌", "紫电幻师", "星耀天宫", "龙云玄帝", 
            "翠岚仙姬", "飞仙剑尊", "雪峰仙师", "修真大道", "仙缘天地", "天命绝尘", "天凤神子", "仙音绝代", "青莲仙尊", "紫云剑帝", 
            "逐风追月", "仙道灵修", "九天神尊", "无影神剑", "梦幻仙境", "风华无双", "龙骧天下", "万古长生", "星梦仙子", "飞天神尊", 
            "神游九天", "仙帝至尊", "云海仙宗", "瑶光仙子", "天骄剑仙", "紫云神尊", "飞天御剑", "霜月仙子"
        };
        int randomIndex = Random.Range(0, names.Length);
        return names[randomIndex];
    }
}